﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;

namespace DataAccess
{
    public class Category
    {
        const string cacheKey = "Category_CachedData";

        public int CategoryID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public Category()
        {
        }

        // Cac ham doc/ghi du lieu 
        public static List<Category> All()
        {
            // lay du lieu tu cache 
            List<Category> data = DataCache.GetCache(cacheKey) as List<Category>;
            if (data == null)
            {
                // neu bang null thi lay du lieu tu CSDL 
                data = CBO.FillCollection<Category>(DataProvider.Instance.ExecuteReader("Category_All"));
                if (data != null && data.Count > 0)
                {
                    data.TrimExcess();
                    // cache lai 
                    DataCache.SetCache(cacheKey, data);
                }
            }
            return data;

            //return CBO.FillCollection<Category>(
            //    DataProvider.Instance.ExecuteReader("Category_All"));
        }

        public static Category Single(string categoryId)
        {
            try
            {
                int id = int.Parse(categoryId);
                return All().Find(delegate(Category c)
                {
                    return c.CategoryID == id;
                });

                //return CBO.FillObject<Category>
                //    (DataProvider.Instance.ExecuteReader("Category_Single", Convert.ToInt32(categoryId)));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool Add(Category category)
        {
            try
            {
                // Neu them thanh cong thi xoa het cache, de lay lai tu dau 
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("@CategoryID", "Category_Add",
                            category.CategoryID, category.Name, category.Description);
                if (Convert.ToInt32(rs) > 0)
                {
                    DataCache.RemoveCache(cacheKey);
                    return true;
                }
                else
                {
                    return false;
                }

                //object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("@CategoryID", "Category_Add",
                //    category.CategoryID, category.Name, category.Description);
                //return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }

        public static bool Update(Category category)
        {
            try
            {
                // Neu cap nhat thanh cong thi xoa het cache, de lay lai tu dau 
                int rs = DataProvider.Instance.ExecuteNonQuery("Category_Update",
                         category.CategoryID, category.Name, category.Description);
                if (rs > 0)
                {
                    DataCache.RemoveCache(cacheKey);
                    return true;
                }
                else
                {
                    return false;
                }

                //int rs = DataProvider.Instance.ExecuteNonQuery("Category_Update", category.CategoryID,
                //    category.Name, category.Description);
                //return rs > 0;
            }
            catch
            {
                return false;
            }
        }

        public static bool Delete(string categoryID)
        {
            try
            {
                // Neu xoa thanh cong thi xoa het cache, de lay lai tu dau 
                int rs = DataProvider.Instance.ExecuteNonQuery("Category_Delete", Convert.ToInt32(categoryID));
                if (rs > 0)
                {
                    DataCache.RemoveCache(cacheKey);
                    return true;
                }
                else
                {
                    return false;
                }

                //int rs = DataProvider.Instance.ExecuteNonQuery("Category_Delete", Convert.ToInt32(categoryID));
                //return rs > 0;
            }
            catch
            {
                return false;
            }
        }
    }
}
